Skip to content

Type-indexed random value generation.#9

Draft
athas wants to merge 1 commit into
masterfrom
gen
Draft

Type-indexed random value generation.#9
athas wants to merge 1 commit into
masterfrom
gen

Conversation

@athas
Copy link
Copy Markdown
Member

@athas athas commented May 29, 2026

The purpose of this PR is to introduce a combinator-based module for generating random values of compound types.

Because of restrictions related to size types, when generating things that involve arrays, you need to split the generation into two parts: generating the sizes, and then generating the values of that size. This can be seen in how the arr generator accepts a specific size.

The actual random number generation piggybacks on the existing generators for primitives.

Here is an example of how to use it to define a generator for random matrices:

module RNG = xorshift128plus
module G = mk_gen RNG

def random_matrix (size: i64) : [][]f64 =
  let rng = RNG.rng_from_seed [i32.i64 size, 1, 2, 3]
  let size = G.fmap (\x -> 1 + (i64.abs x) % size) G.i64
  let (rng, (n, m)) = G.generate rng (G.pair size size)
  let (_, M) = G.generate rng (G.arr (n * m) G.f64)
  in unflatten M

This generator produces random matrices of size up to size. It's easy enough to make it generate random matrices of exactly size, of course.

The purpose of this PR is to introduce a combinator-based module for generating
random values of compound types.
@athas
Copy link
Copy Markdown
Member Author

athas commented May 29, 2026

@melsman What do you think of this design?

@athas athas marked this pull request as draft May 29, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant